<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\model\MonthCardTemplate;
use app\common\model\NumberCardTemplate;
use app\common\model\ViperCardSupplement;
use app\common\service\AddService;
use app\common\service\OrdinaryService;
use app\web\constant\CommonConstant;
use think\Db;

/**
 * 单据控制器
 * @package app\index\controller
 */
class Checkout extends Auth
{
    public $noNeedLogin = [];

    /**
     * 订单明细列表
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order_list()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            $id = $post['id'] ?? 0;//给查看详情使用
            $totalClock = Db::name('qt_check_out_item')
                ->alias('oi')
                ->leftJoin('qt_check_out o','o.id = oi.check_out_id')
                ->where('o.store_id', $this->userInfo['store_id'])->where('oi.link_type','1');//总钟数
            $totalPayCount = Db::name('qt_check_out_item')
                ->alias('oi')
                ->leftJoin('qt_check_out o','o.id = oi.check_out_id')
                ->where('o.store_id', $this->userInfo['store_id'])
                ->where('oi.link_type','1')
                ->where('o.pay_status','1')
                ->group('check_out_id');//总支付订单数
            $totalPayClockCount = Db::name('qt_check_out_item')
                ->alias('oi')
                ->leftJoin('qt_check_out o','o.id = oi.check_out_id')
                ->where('o.store_id', $this->userInfo['store_id'])
                ->where('o.pay_status','1')
                ->where('oi.link_type','1');//总支付钟数
            $totalUnpaidCount = Db::name('qt_check_out_item')
                ->alias('oi')
                ->leftJoin('qt_check_out o','o.id = oi.check_out_id')
                ->where('o.store_id', $this->userInfo['store_id'])
                ->where('o.pay_status','2')
                ->where('oi.link_type','1')->group('check_out_id');//总未支付订单数
            $totalUnpaidClockCount = Db::name('qt_check_out_item')
                ->alias('oi')
                ->leftJoin('qt_check_out o','o.id = oi.check_out_id')
                ->where('o.store_id', $this->userInfo['store_id'])
                ->where('o.pay_status','2')
                ->where('oi.link_type','1');//总未支付订单数
            $query = Db::name('qt_check_out')
                ->alias('out')
                ->field([
                    'out.id',//结账单ID
                    'out.order_no',//账单号
                    'out.is_revoke',//状态
                    'out.revoke_time',//撤销时间
                    'out.revoke_person',//撤销人
                    'out.revoke_remarks',//撤销备注
                    'out.cashier_id',//结账人
                    'out.cashier_time',//结账时间
                    'out.pay_content',//付款方式
                    'out.total_money',//总金额
                    'out.payment_money',//实际支付
                    'out.project_order',//项目订单
                    'out.goods_order',//商品订单

                ])->where('out.store_id', $this->userInfo['store_id']);


            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('out.cashier_time', $startTime, $endTime);

                $totalClock->whereBetweenTime('cashier_time', $startTime, $endTime);
                $totalPayCount->whereBetweenTime('cashier_time', $startTime, $endTime);
                $totalPayClockCount->whereBetweenTime('cashier_time', $startTime, $endTime);
                $totalUnpaidCount->whereBetweenTime('cashier_time', $startTime, $endTime);
                $totalUnpaidClockCount->whereBetweenTime('cashier_time', $startTime, $endTime);
            }

            if (!empty($id)) {
                $query->where('out.id', $id);
            }

            //账单单号
            if (!empty($post['order_no'])) {
                $query->where('out.order_no', $post['order_no']);

                $totalClock->where('o.order_no', $post['order_no']);
                $totalPayCount->where('o.order_no', $post['order_no']);
                $totalPayClockCount->where('o.order_no', $post['order_no']);
                $totalUnpaidCount->where('o.order_no', $post['order_no']);
                $totalUnpaidClockCount->where('o.order_no', $post['order_no']);

            }
            //账单状态
            if (!empty($post['is_revoke'])) {
                $query->where('out.is_revoke', $post['is_revoke']);
                $totalClock->where('o.is_revoke', $post['is_revoke']);
                $totalPayCount->where('o.is_revoke', $post['is_revoke']);
                $totalPayClockCount->where('o.is_revoke', $post['is_revoke']);
                $totalUnpaidCount->where('o.is_revoke', $post['is_revoke']);
                $totalUnpaidClockCount->where('o.is_revoke', $post['is_revoke']);
            }
            //状态
            if (!empty($post['status'])) {
                $order_project_id = Db::name('qt_order_project')->where('status', $post['status'])->column('id');
                $query->whereIn('out.project_order', $order_project_id);
                $totalClock->whereIn('o.project_order', $order_project_id);
                $totalPayCount->whereIn('o.project_order', $order_project_id);
                $totalPayClockCount->whereIn('o.project_order', $order_project_id);
                $totalUnpaidCount->whereIn('o.project_order', $order_project_id);
                $totalUnpaidClockCount->whereIn('o.project_order', $order_project_id);
            }

            $data = $query->order('out.id desc')->paginate($limit)->each(function ($item)  {

                $item['project_detail'] = [];//项目
                $item['product_detail'] = [];//商品
                $item['revoke_person_name'] = Db::name('qt_users')->where('id', $item['revoke_person'])->value('real_name');//撤销人
                $item['cashier_name'] = Db::name('qt_users')->where('id', $item['cashier_id'])->value('real_name');//结账人

                $item_data = Db::name('qt_check_out_item')->where('check_out_id', $item['id'])->select();
                if (empty($item_data)) {
                    return $item;
                }

                foreach ($item_data as $key => &$val) {

                    $data = [];
                    //项目
                    if (!empty($val['link_type']) && $val['link_type'] == 1) {

                        $project_data = Db::name('qt_order_project')
                            ->where('project_id', $val['link_id'])
                            ->whereIn('order_id', explode(',', $item['project_order']))
                            ->column('id,room_id,engineer_id,project_id,hand_card_id,old_card_id,name as project_name,order_type,
                            is_add,is_change,created_time,start_time,real_end_time,end_time,num,price,payment_price,status', 'project_id');

                        if (empty($project_data[$val['link_id']])) {
                            continue;
                        }

                        $project_item = $project_data[$val['link_id']];
                        $data['project_name'] = $project_item['project_name'] ?? '';
                        $engineer_data = Db::name('qt_users')->where('id', $project_item['engineer_id'])->find();
                        $data['engineer_name'] = $engineer_data['real_name'] ?? '';
                        $data['work_number'] = $engineer_data['work_number'] ?? '';
                        $data['engineer_id'] = $project_item['engineer_id'] ?? '';
                        $data['hand_card_name'] = Db::name('qt_hand_card')->where('id', $project_item['hand_card_id'])->value('name') ?? '';
                        $data['old_hand_card_name'] = Db::name('qt_hand_card')->where('id', $project_item['old_card_id'])->value('name') ?? '';

                        $data['room_id'] = $project_item['room_id'] ?? '';
                        $data['room_name'] = Db::name('qt_room')->where('id', $project_item['room_id'])->value('name') ?? '';
                        $data['order_type'] = $project_item['order_type'] ?? '';
                        $data['is_add'] = $project_item['is_add'] ?? '';

                        $data['created_time'] = $project_item['created_time'] ?? '';
                        $data['start_time'] = $project_item['start_time'] ?? '';
                        $data['real_end_time'] = $project_item['real_end_time'] ?? '';
                        $data['end_time'] = $project_item['end_time'] ?? '';
                        $data['pay_time'] = '';
                        $data['num'] = $project_item['num'] ?? '';
                        $data['total_price'] = $project_item['price'] ?? '';
                        $data['payment_price'] = $project_item['payment_price'] ?? '';
                        $item['project_detail'][] = $data;
                    }

                    //商品
                    if (!empty($val['link_type']) && $val['link_type'] == 2) {

                        $product_data = Db::name('qt_product_order_item')
                            ->alias('i')
                            ->leftJoin('qt_product_order o', 'i.order_id=o.id')
                            ->whereIn('o.id', explode(',', $item['goods_order']))//订单ID
                            ->where('i.goods_id', $val['link_id'])//订单ID
                            ->where(['i.status' => '1'])
                            ->column('o.id,o.room_id,o.engineer_id,i.goods_id as project_id,o.hand_card_id,o.old_card_id,
                            i.goods_name as project_name,o.create_time,o.pay_time,o.total_price,o.payment_price,i.business_type', 'i.goods_id');

                        if (empty($product_data[$val['link_id']])) {
                            continue;
                        }

                        $product_item = $product_data[$val['link_id']];

                        $data['project_name'] = $product_item['project_name'] ?? '';
                        $engineer_data = Db::name('qt_users')->where('id', $product_item['engineer_id'])->find();//技师
                        $data['engineer_name'] = $engineer_data['real_name'] ?? '';
                        $data['work_number'] = $engineer_data['work_number'] ?? '';
                        $data['engineer_id'] = $product_item['engineer_id'] ?? '';
                        $data['hand_card_name'] = Db::name('qt_hand_card')->where('id', $product_item['hand_card_id'])->value('name') ?? '';
                        $data['old_hand_card_name'] = Db::name('qt_hand_card')->where('id', $product_item['old_card_id'])->value('name') ?? '';
                        $data['room_id'] = $product_item['room_id'] ?? '';
                        $data['room_name'] = Db::name('qt_room')->where('id', $product_item['room_id'])->value('name') ?? '';
                        $data['order_type'] = '';
                        $data['is_add'] = '';

                        $data['created_time'] = $product_item['created_time'] ?? '';
                        $data['start_time'] = '';
                        $data['real_end_time'] = '';
                        $data['end_time'] = '';
                        $data['pay_time'] = $product_item['pay_time'] ?? '';

                        $data['num'] = $product_item['goods_num'] ?? '';
                        $data['total_price'] = $product_item['total_price'] ?? '';
                        $data['business_type'] = $product_item['business_type'] ?? '';
                        $data['payment_price'] = $product_item['payment_price'] ?? '';
                        $item['product_detail'][] = $data;
                    }
                }

                $item['item_data'] = $item_data;
                return $item;
            });
        }
        $result['data'] = $data;
//        $totalClock->whereIn('project_order', $order_project_id);
//        $totalPayCount->whereIn('project_order', $order_project_id);
//        $totalPayClockCount->whereIn('project_order', $order_project_id);
//        $totalUnpaidCount->whereIn('project_order', $order_project_id);
//        $totalUnpaidClockCount->whereIn('project_order', $order_project_id);
        $result['total_clock'] = $totalClock->count();
        $result['total_pay_count']  = $totalPayCount->count();
        $result['total_pay_clock_count']= $totalPayClockCount->count();
        $result['total_unpaid_count']= $totalUnpaidCount->count();
        $result['total_unpaid_clock_count']= $totalUnpaidClockCount->count();
        $this->result('查询成功', $result, 200);


    }


    /**
     * 账单明细列表
     */
    public function out_order_detail()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            $query = Db::name('qt_check_out_item')->alias('item')
                ->field([
                    'item.*',
                    'out.order_no',//账单号
                    'out.is_revoke',//状态
                    'out.revoke_time',//撤销时间
                    'out.revoke_person',//撤销人
                    'out.revoke_remarks',//撤销备注
                    'out.cashier_id',//结账人
                    'out.cashier_time',//结账时间
                    'out.pay_content',//付款方式
                    'out.total_money',//总金额
                    'out.payment_money',//实际支付
                    'out.project_order',//项目订单
                    'out.goods_order',//商品订单
                    'out.open_room_time',//开放时间
                ])
                ->leftJoin('qt_check_out out', 'item.check_out_id=out.id')
                ->where('out.store_id', $this->userInfo['store_id']);

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                //$query = $query->whereBetweenTime('out.cashier_time', $startTime, $endTime);
                $query->where('out.cashier_time', '>=', $startTime)
                    ->where('out.cashier_time', '<=', $endTime);
            }
            //账单Id
            if (isset($post['id']) && !empty($post['id'])) {
                $query->where('out.id', $post['id']);
            }
            //账单单号
            if (!empty($post['order_no'])) {
                $query->where('out.order_no', $post['order_no']);
            }
            //账单状态
            if (!empty($post['is_revoke'])) {
                $query->where('out.is_revoke', $post['is_revoke']);
            }

            $where = [];
            $product_where = [];
            if (!empty($post['room_id'])) {
                $where['room_id'] = $post['room_id'];
                $product_where['o.room_id'] = $post['room_id'];
            }
            if (!empty($post['engineer_id'])) {
                $where['engineer_id'] = $post['engineer_id'];
                $product_where['o.engineer_id'] = $post['engineer_id'];
            }
            if (!empty($post['hand_card_id'])) {
                $where['hand_card_id'] = $post['hand_card_id'];
                $product_where['o.hand_card_id'] = $post['hand_card_id'];
            }

            $total_num = $query->sum('total_num');
            $actual_receipt = $query->sum('actual_receipt');
            $receivable = $query->sum('receivable');

            $data = $query->order('item.id desc')->paginate($limit)
                ->each(function ($item) use ($where, $product_where) {
                    $item['revoke_person_name'] = Db::name('qt_users')->where('id', $item['revoke_person'])->value('real_name');//撤销人
                    $item['cashier_name'] = Db::name('qt_users')->where('id', $item['cashier_id'])->value('real_name');//结账人
                    //项目
                    if ($item['link_type'] == 1) {

                        $project_data = Db::name('qt_order_project')
                            ->where('project_id', $item['link_id'])
                            ->where($where)
                            ->whereIn('order_id', explode(',', $item['project_order']))
                            ->column('id,room_id,engineer_id,project_id,hand_card_id,name as project_name,order_type,is_add,num,price,
                        payment_price,start_time,real_end_time,created_time', 'project_id');

                        if (!empty($project_data[$item['link_id']])) {
                            $project_item = $project_data[$item['link_id']];
                            $item['project_name'] = $project_item['project_name'] ?? '';
                            $engineer_data = Db::name('qt_users')->where('id', $project_item['engineer_id'])->find();
                            $item['engineer_name'] = $engineer_data['real_name'] ?? '';
                            $item['work_number'] = $engineer_data['work_number'] ?? '';
                            $item['engineer_id'] = $project_item['engineer_id'] ?? '';
                            $item['hand_card_name'] = Db::name('qt_hand_card')->where('id', $project_item['hand_card_id'])->value('name') ?? '';

                            $item['room_id'] = $project_item['room_id'] ?? '';
                            $item['room_name'] = Db::name('qt_room')->where('id', $project_item['room_id'])->value('name') ?? '';
                            $item['order_type'] = $project_item['order_type'] ?? '';
                            $item['is_add'] = $project_item['is_add'] ?? '';

                            $item['num'] = $project_item['num'] ?? '';
                            $item['total_price'] = $project_item['price'] ?? '';
                            $item['payment_price'] = $project_item['payment_price'] ?? '';
                            $item['created_time'] = $project_item['created_time'] ?? 0;
                            $item['real_end_time'] = $project_item['real_end_time'] ?? 0;
                            $item['start_time'] = $project_item['start_time'] ?? 0;

                        }
                    }


                    //商品
                    if ($item['link_type'] == 2) {

                        $product_data = Db::name('qt_product_order_item')
                            ->alias('i')
                            ->leftJoin('qt_product_order o', 'i.order_id=o.id')
                            ->whereIn('o.id', explode(',', $item['goods_order']))//订单ID
                            ->where('i.goods_id', $item['link_id'])//订单ID
                            ->where($product_where)
                            ->where(['i.status' => '1'])
                            ->column('o.id,o.room_id,o.engineer_id,i.goods_id as project_id,o.hand_card_id,
                        i.goods_name as project_name,i.goods_price,i.goods_num,
                        o.total_price,o.payment_price,i.create_time,i.business_type', 'i.goods_id');

                        if (!empty($product_data[$item['link_id']])) {
                            $product_item = $product_data[$item['link_id']];
                            $item['project_name'] = $product_item['project_name'] ?? '';
                            $engineer_data = Db::name('qt_users')->where('id', $product_item['engineer_id'])->find();//技师
                            $item['engineer_name'] = $engineer_data['real_name'] ?? '';
                            $item['work_number'] = $engineer_data['work_number'] ?? '';
                            $item['engineer_id'] = $product_item['engineer_id'] ?? '';
                            $item['hand_card_name'] = Db::name('qt_hand_card')->where('id', $product_item['hand_card_id'])->value('name') ?? '';
                            $item['room_id'] = $product_item['room_id'] ?? '';
                            $item['room_name'] = Db::name('qt_room')->where('id', $product_item['room_id'])->value('name') ?? '';
                            $item['order_type'] = '';
                            $item['is_add'] = '';
                            $item['num'] = $product_item['goods_num'] ?? '';
                            $item['total_price'] = $product_item['goods_price'] ?? '';
                            $item['payment_price'] = $product_item['payment_price'] ?? '';
                            $item['create_time'] = $product_item['create_time'] ?? '';
                            $item['business_type'] = $product_item['business_type'] ?? '';
                        }
                    }

                    return $item;
                });
        }

        $this->result('查询成功', [
            'item' => $data,
            'total' => [
                'total_num' => $total_num,
                'total_receivable' => $receivable,
                'actual_receipt' => $actual_receipt]
        ], 200);


    }

    /**
     * 编辑账单明细列表
     */
    public function update_out_order()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $userInfo = $this->userInfo;

            if (empty($post['id'])) {
                $this->result('ID不能为空', '', 204);
            }
            //结账单中的房间id
            $check_out_data = Db::name('qt_check_out')
                ->where('id', $post['id'])
                ->where('store_id', $this->userInfo['store_id'])
                ->find();
            if (empty($check_out_data)) {
                $this->result('没有找到对应ID的数据', '', 204);
            }
            $room_ids = Db::name('qt_order')->whereIn('id', explode(',', $check_out_data['project_order']))
                ->where('store_id', $this->userInfo['store_id'])->column('room_id');
            $room_ids = array_unique($room_ids);

            $data = [];
            if (!empty($post['is_revoke'])) {
                $data['is_revoke'] = $post['is_revoke'];
                $data['pay_status'] = 2;
                $data['revoke_person'] = $userInfo['id'];
                $data['revoke_time'] = time();

                $store_info = Db::name('qt_store')->where(['id' => $userInfo['store_id']])->find();
                if (!empty($store_info['cancel_order_block']) && !empty($check_out_data['cashier_time'])) {
                    if ($check_out_data['cashier_time'] < (time() - $store_info['cancel_order_block'] * 3600)) {
                        $this->result('只能撤销' . $store_info['cancel_order_block'] . ' 小时的订单', '', 204);
                    }
                }

                //$order_queue_data = Db::name('qt_order_queue')->whereIn('id',$room_ids)->find();
                //if (empty($order_queue_data)) {
                //    $this->result('技师已经下钟不能撤销！', '', 204);
                //}
                if (!empty($room_ids)) {
                    //空闲可撤销
                    $is_empty_room = Db::name('qt_room')->whereIn('id', $room_ids)->where('status_id', '<>', 1)->find();
                    if (!empty($is_empty_room)) {//如果房间不是空闲则判断房间是否支付，不是未支付的，不能撤销
                        $order_data = Db::name('qt_order')->whereIn('id', explode(',', $check_out_data['project_order']))->where('pay_status', '<>', 1)->select();
                        $product_data = Db::name('qt_product_order')->whereIn('id', explode(',', $check_out_data['goods_order']))->where('pay_status', '<>', 1)->select();
                        if (!empty($order_data) || !empty($product_data)) {
                            $this->result('订单已支付或者挂账不能撤销！', '', 204);
                        }
                    }
                }
            }

            if (!empty($post['pay_content'])) {
                $data['pay_content'] = $post['pay_content'];
            }
            if (!empty($post['revoke_remarks'])) {
                $data['revoke_remarks'] = $post['revoke_remarks'];
            }

            Db::startTrans();

            try {
                Db::name('qt_check_out')->where('id', $post['id'])->where('store_id', $this->userInfo['store_id'])->update($data);

                if (!empty($post['is_revoke'])) {
                    //修改项目订单状态
                    Db::name('qt_order')->whereIn('id', explode(',', $check_out_data['project_order']))->update(['pay_status' => 1]);
                    Db::name('qt_order_project')->whereIn('order_id', explode(',', $check_out_data['project_order']))->update(['pay_status' => 0]);

                    //修改商品订单状态
                    Db::name('qt_product_order')->whereIn('id', explode(',', $check_out_data['goods_order']))->update(['pay_status' => 1, 'order_status' => 0]);
                    Db::name('qt_product_order_item')->whereIn('order_id', explode(',', $check_out_data['goods_order']))->update(['pay_status' => 0]);

                    //更新房间状态可支付
                    Db::name('qt_room')->whereIn('id', $room_ids)->update(['status_id' => $check_out_data['room_status']]);

                    Db::name('qt_check_out_pay')->where('check_out_id', $post['id'])->where('store_id', $this->userInfo['store_id'])->delete();

                }

                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->result('请求错误' . $e->getMessage(), '', 204);
            }

            $this->result('修改成功', '', 200);
        }

        $this->result('请求错误', $data, 204);

    }


    /**
     * 结账简明列表
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public function check_out_list()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = $this->request->post('limit') ?? 10;
            $sql = Db::name('qt_check_out')->alias('c')
                ->field([
                    'c.id',
                    'c.order_no',
                    'c.total_money',
                    'c.payment_money',
                    'c.pay_content',
                    'u.real_name',
                    'c.cashier_time',
                    'c.cashier_content',
                    'c.project_order',
                    'c.activity_id',
                    'c.is_revoke'
                    //'o.room_id',
                    //'o.hand_card_id',
                    //'o.id as order_id',

                ])
                //->leftJoin('qt_order o ', 'o.id = c.project_order')
                ->leftJoin('qt_users u ', 'u.id = c.cashier_id')
                ->where('c.store_id', $this->userInfo['store_id']);

            $totalPaymentMoneySql = Db::name('qt_check_out')->where('is_revoke', 2);
            $totalMoneySql = Db::name('qt_check_out')->where('is_revoke', 2);
            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $sql->whereBetweenTime('c.cashier_time', $startTime, $endTime);
                $totalPaymentMoneySql->whereBetweenTime('cashier_time', $startTime, $endTime);
                $totalMoneySql->whereBetweenTime('cashier_time', $startTime, $endTime);
            }
            //支付方式
            if (isset($post['pay_type']) && !empty($post['pay_type'])) {
                $pay_type_sql = '';
                $pay_type = $post['pay_type'];
                foreach ($pay_type as $key => $value) {
                    $pay_type_sql .= 'c.pay_content like "%' . $value . '%" or ';
                }
                $totalTypeSql = '';
                foreach ($pay_type as $key => $value) {
                    $totalTypeSql .= 'c.pay_content like "%' . $value . '%" or ';
                }
                $sql->where(rtrim($pay_type_sql, 'or '));
                $totalPaymentMoneySql->where(rtrim($totalTypeSql, 'or '));
                $totalMoneySql->where(rtrim($totalTypeSql, 'or '));
            }
            //活动
            if (isset($post['activity_id']) && !empty($post['activity_id'])) {
                $sql->where('c.activity_id', $post['activity_id']);
                $totalPaymentMoneySql->where('c.activity_id', $post['activity_id']);
                $totalMoneySql->where('activity_id', $post['activity_id']);
            }
            //账单号
            if (isset($post['order_no']) && !empty($post['order_no'])) {
                $sql->whereLike('c.order_no', '%' . $post['order_no'] . '%');
                $totalPaymentMoneySql->whereLike('order_no', '%' . $post['order_no'] . '%');
                $totalMoneySql->whereLike('order_no', '%' . $post['order_no'] . '%');
            }
            //房间号
            if (isset($post['room_id']) && !empty($post['room_id'])) {
                $sql->where('c.room_id', $post['room_id']);
                $totalPaymentMoneySql->where('room_id', $post['room_id']);
                $totalMoneySql->where('room_id', $post['room_id']);
            }
            //技师号
            if (isset($post['engineer_id']) && !empty($post['engineer_id'])) {
                $sql->where("FIND_IN_SET(" . $post['engineer_id'] . ",engineer_ids)");
                $totalPaymentMoneySql->where("FIND_IN_SET(" . $post['engineer_id'] . ",engineer_ids)");
                $totalMoneySql->where("FIND_IN_SET(" . $post['engineer_id'] . ",engineer_ids)");
            }
            //撤销状态
            if (isset($post['is_revoke']) && !empty($post['is_revoke'])) {
                $sql->where('c.is_revoke', $post['is_revoke']);
                $totalPaymentMoneySql->where('is_revoke', $post['is_revoke']);
                $totalMoneySql->where('is_revoke', $post['is_revoke']);
            }
            $data = $sql->order('c.id desc')->paginate($limit)->each(function ($item) {
                //查询结账明细
                $check_out_item = Db::name('qt_order_project')
                    ->alias('p')
                    ->field([
                        'id',
                        'room_id',
                        'hand_card_id',
                        'engineer_id',
                    ])->whereIn('order_id', explode(',', $item['project_order']))->select();
                //echo "</pre>";
                //print_r($check_out_item);
                //exit;
                $engineerNumber = '';
                $room_name = '';
                $hand_card = '';
                if (!empty($check_out_item)) {
                    foreach ($check_out_item as $key => $value) {
                        $engineerNumber .= Db::name('qt_users')->where('id', $value['engineer_id'])->value('work_number') . ',';
                        $room_name .= Db::name('qt_room')->where('id', $value['room_id'])->value('name') . ',';
                        $hand_card .= Db::name('qt_hand_card')->where('id', $value['hand_card_id'])->value('name') . ',';
                    }
                }

                $payContent = '';
                $content = json_decode($item['pay_content'], true);
                if (!empty($content)) {
                    foreach ($content as $value) {
                        $payContent .= $value['pay_type'] . ',';
                    }
                }
                $item['room_name'] = rtrim($room_name, ',');
                $item['engineer_name'] = rtrim($engineerNumber, ',');
                $item['hand_card'] = rtrim($hand_card, ',');
                $item['pay_content'] = rtrim($payContent, ',');
                return $item;
            });
        }
        $total['total_money'] = $totalMoneySql->sum('total_money');
        $total['payment_money'] = $totalPaymentMoneySql->sum('payment_money');
        $result['data'] = $data;
        $result['total'] = $total;
        $this->result('查询成功', $result, 200);
    }


    /**
     * 结账简明列表详情
     */
    public function check_out_detail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $data = Db::name('qt_check_out')->alias('c')
                ->field([
                    'c.id',
                    'c.order_no',
                    'c.total_money',
                    'c.payment_money',
                    'c.pay_content',
                    'u.real_name',
                    'c.cashier_time',
                    'c.cashier_content',
                    'c.project_order',
                    'c.activity_id',
                    'c.goods_order',
                    'c.is_revoke',
                ])
                ->leftJoin('qt_users u ', 'u.id = c.cashier_id')
                ->where('c.id',$getData['id'])
                ->find();
            $goodsOrder = [];
            $projectOrder = [];
            if(!empty($data['project_order'])){
                $projectOrder = Db::name('qt_order_project')
                    ->field('r.name as room_name,u.work_number,h.name as hand_card_name,op.name as project_name,op.order_type,op.created_time,op.start_time,op.real_end_time,op.end_time,op.num,op.price,op.payment_price')
                    ->alias('op')
                    ->leftJoin('qt_room r','r.id = op.room_id')
                    ->leftJoin('qt_hand_card h','h.id = op.hand_card_id')
                    ->leftJoin('qt_users u','u.id = op.engineer_id')
                    ->where(['op.order_id'=>$data['project_order']])
                    ->select();
            }
            if(!empty($data['goods_order'])){
                $goodsOrder = Db::name('qt_product_order_item')
                    ->field('r.name as room_name,h.name as hand_card_name,op.goods_name as project_name,op.create_time as created_time,op.goods_num as num,op.goods_total_price as price,op.payment_price')
                    ->alias('op')
                    ->leftJoin('qt_room r','r.id = op.room_id')
                    ->leftJoin('qt_hand_card h','h.id = op.hand_card_id')
                    ->where(['op.order_id'=>$data['project_order']])
                    ->select();
                foreach ($goodsOrder as $key=>$value){
                    $goodsOrder[$key]['order_type'] = '';
                    $goodsOrder[$key]['start_time'] = null;
                    $goodsOrder[$key]['real_end_time'] = null;
                    $goodsOrder[$key]['end_time'] = null;
                }
            }
            $data['order_product'] = array_merge($projectOrder,$goodsOrder);
            $this->result('查询成功',$data , 200);
        }
    }


    /**
     * 订单提成列表
     */
    public function order_commission_list()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = $this->request->post('limit', 10);
            $query = Db::name('qt_order_project')
                ->alias('op')
                ->field('op.*,r.name as room_name,o.order_no,o.pay_time,o.id as order_id,o.hand_card_id,op.created_time,p.name as project_name,op.creater_room,op.start_time,op.real_end_time,op.end_time')
                ->leftJoin('qt_order o ', 'o.id = op.order_id')
                ->leftJoin('qt_room r ', 'r.id = op.room_id')
                ->leftJoin('qt_project p', 'p.id = op.project_id')
                ->leftJoin('qt_users u', 'op.engineer_id = u.id')
                //->leftJoin('qt_commission_log log', 'log.product_id = op.project_id and log.type = 1')
                ->where('op.store_id', $this->userInfo['store_id']);
            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('op.created_time', $startTime, $endTime);
            }
            //手牌ID
            if (!empty($post['hand_card_id'])) {
                $query->where('o.hand_card_id', $post['hand_card_id']);
            }
            //技师ID
            if (!empty($post['work_number'])) {
                $query->where('u.work_number', $post['work_number']);
            }
            //房间id
            if (!empty($post['room_id'])) {
                $query->where('r.name', $post['room_id']);
            }
            //开房人
            if (!empty($post['created_id'])) {
                $query->where('op.created_order_id', $post['created_id']);
            }
            //项目id
            if (!empty($post['project_id'])) {
                $query->where('op.project_id', $post['project_id']);
            }
            //订单类型
            if (!empty($post['order_type'])) {
                $query->where('op.order_type', $post['order_type']);
            }
            $data = $query->order('op.id desc')->paginate($limit)->each(function ($item) {
                $item['created_order_name'] = Db::name('qt_users')->where('id', $item['created_order_id'])->value('real_name') ?? '';
                $engineer_data = Db::name('qt_users')->where('id', $item['engineer_id'])->find();
                $item['engineer_name'] = !empty($engineer_data['real_name']) ? $engineer_data['real_name'] : '';
                $item['work_number'] = !empty($engineer_data['work_number']) ? $engineer_data['work_number'] : '';
                //$item['engineer_name'] = Db::name('qt_users')->where('id', $item['engineer_id'])->value('real_name') ?? '';
                $item['create_room_name'] = Db::name('qt_users')->where('id', $item['creater_room'])->value('real_name') ?? '';
                $item['hand_card_name'] = Db::name('qt_hand_card')->where('id', $item['hand_card_id'])->value('name') ?? '';

                $check_out = Db::name('qt_check_out')->where("FIND_IN_SET(" . $item['order_id'] . ",project_order)")->find();
                $item['check_out_no'] = !empty($check_out['order_no']) ? $check_out['order_no'] : '';
                $item['cashier_time'] = !empty($check_out['cashier_time']) ? date('Y-m-d H:i:s', $check_out['cashier_time']) : '';
                return $item;
            });

            $this->result('查询成功', $data, 200);
        }

        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改订单提成数据
     */
    public function update_order_commission()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $id = $post['id'];
            $project = Db::name('qt_order_project')->where('store_id', $this->userInfo['store_id'])->where('id', $id)->find();

            if (empty($project)) {
                $this->result('没有找到对应ID的数据', '', 204);
            }

            if (!empty($post['created_order_id']) || !empty($post['order_type']) || !empty($post['is_add']) || !empty($post['creater_room'])) {
                Db::name('qt_order_project')->where('store_id', $this->userInfo['store_id'])->where('id', $post['id'])->update($post);

                $projectRes = Db::name('qt_project')->where('id', $project['project_id'])->find();
                $is_add = !empty($post['is_add']) ? $post['is_add'] : $project['is_add'];
                $order_type = !empty($post['order_type']) ? $post['order_type'] : $project['order_type'];

                //普通提成
                if ((!empty($post['order_type']) || !empty($post['is_add'])) && $is_add == '1') {
                    $commission = new OrdinaryService();
                    $post['commission'] = $commission->showCommission($order_type, $is_add, $projectRes);
                }
                //加班提成
                if ((!empty($post['order_type']) || !empty($post['is_add'])) && $is_add == '2') {
                    $commission = new AddService();
                    $post['commission'] = $commission->showCommission($order_type, $is_add, $projectRes);
                }
            }

            if (!empty($post['commission']) || !empty($post['receiver'])) {
                unset($post['id']);
                Db::name('qt_order_project')
                    ->where('store_id', $this->userInfo['store_id'])
                    ->where('id', $id)->update($post);

                Db::name('qt_commission_log')->where('store_id', $this->userInfo['store_id'])
                    ->where('product_id', $project['project_id'])->where('type', 1)->update($post);
            }
            $this->result('修改完成', '', 200);
        }

        $this->result('无效的请求', '', 204);
    }


    /**
     * 商品明细列表
     */
    public function goods_info_list()
    {
        //结账单号，房间号，手牌号，商品名称，数量，金额，付款方式，销售人，提成，下单时间，结账时间，下单人
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            $query = Db::name('qt_product_order_item')->alias('oi')
                ->field([
                    "oi.id",//id
                    "oi.order_id",//id
                    "po.pay_no",//结账单号
                    "po.room_id",//房间号
                    "po.hand_card_id",//手牌号
                    "po.payment_price",//支付金额
                    "po.pay_type",//支付方式 1 微信2 支付宝 3余额
                    "po.pay_time",//支付时间。结账时间，
                    "po.uv_id",//下单人
                    "po.pay_status",//支付状态 0 待支付 1 已支付
                    "oi.goods_id",
                    "oi.goods_name",
                    "oi.goods_num",
                    "oi.create_time",//下单时间
                    "po.create_id",//下单时间
                    "oi.sale_id",//销售人
                    "oi.commission",//提成
                    "oi.goods_price",//单价金额
                    "oi.goods_total_price",//总价金额
                    "oi.business_type",//业务类型 1商品 2房费商品
                    "oi.status"
                ])
                ->leftJoin('qt_product_order po', 'po.id=oi.order_id')
                ->where('oi.store_id', $this->userInfo['store_id']);

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('oi.create_time', $startTime, $endTime);
            }
            //商品Id
            if (!empty($post['goods_id'])) {
                $query->whereIn('oi.goods_id', explode(',', $post['goods_id']));
            }
            //房间号
            if (!empty($post['room_id'])) {
                $room_id = Db::name('qt_room')->where('id', $post['room_id'])->value('id');
                if ($room_id) {
                    $query->where('po.room_id', $room_id);
                }
            }

            //手牌ID
            if (!empty($post['hand_card_id'])) {
                $hand_card_id = Db::name('qt_hand_card')->whereLike('name', '%' . $post['hand_card_id'] . '%')->column('id');
                if ($hand_card_id) {
                    $query->whereIn('po.hand_card_id', $hand_card_id);
                } else {
                    $query->where('po.hand_card_id', 0);
                }
            }
            //销售人
            if (!empty($post['sale_id'])) {
                $query->where('oi.sale_id', $post['sale_id']);
            }
            //下单人
            if (!empty($post['create_id'])) {
                $query->where('po.create_id', $post['create_id']);
            }
            //支付状态
            if (!empty($post['pay_status'])) {
                $query->where('po.pay_status', $post['pay_status']);
            }

            //订单状态
            if (isset($post['status']) && $post['status'] == '1') {
                $query->where('oi.status', $post['status']);
            }
            $list = $query->order('oi.id desc')->paginate($limit)->each(function ($item) {
                $item['sale_user_name'] = Db::name('qt_users')->where('id', $item['sale_id'])->value('real_name') ?? '';
                $item['create_user_name'] = Db::name('qt_users')->where('id', $item['create_id'])->value('real_name') ?? '';
                $item['uv_name'] = Db::name('qt_users')->where('id', $item['uv_id'])->value('real_name') ?? '';
                $item['pay_type_name'] = !empty($item['pay_type']) ? CommonConstant::$product_order_pay_type[$item['pay_type']] : '';
                $item['room_name'] = Db::name('qt_room')->where('id', $item['room_id'])->value('name') ?? '';
                $item['hand_card_name'] = Db::name('qt_hand_card')->where('id', $item['hand_card_id'])->value('name') ?? '';
                $check_out = Db::name('qt_check_out')->where("FIND_IN_SET(" . $item['order_id'] . ",goods_order)")->find();
                $item['check_out_no'] = !empty($check_out['order_no']) ? $check_out['order_no'] : '';
                $item['cashier_time'] = !empty($check_out['cashier_time']) ? date('Y-m-d H:i:s', $check_out['cashier_time']) : '';

                return $item;
            });

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * 编辑商品明细列表销售人，提成
     */
    public function edit_goods_info()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (!empty($post['id'])) {
                if (isset($post['sale_id']) && !empty($post['sale_id'])) {
                    $data['sale_id'] = $post['sale_id'];
                }

                if (!empty($post['commission'])) {
                    $data['commission'] = $post['commission'];
                }

                $item_data = Db::name('qt_product_order_item')
                    ->where('store_id', $this->userInfo['store_id'])
                    ->where('id', $post['id'])
                    ->where(['status' => '1'])->find();
                if (empty($item_data)) {
                    $this->result('没有找到对应ID的商品明细数据', '', 204);
                }

                Db::name('qt_product_order_item')->where('id', $post['id'])->update($data);

                //更新提成记录
                if (!empty($post['commission'])) {
                    Db::name('qt_commission_log')
                        ->where([
                            'order_project_id' => $post['id'],
                            'product_id' => $item_data['goods_id'],
                            'type' => 2,
                        ])
                        ->update(['commission' => $data['commission']]);
                }


                $this->result('修改成功', '', 200);
            }
            $this->result('无效的请求', '', 204);
        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * 订房明细列表
     */
    public function room_appointment_list()
    {

        if ($this->request->isPost()) {

            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;

            $query = Db::name('qt_room_book')->alias('rb')
                ->field([
                    'rb.id',
                    'rb.status',//状态 是否到达 1 未到达 2 已到达 3 已取消
                    'rb.room_id',//房间
                    'rb.time_out',//预订时间/分钟
                    'rb.mobile',//客人电话  +
                    'u.group',//销售类型 +
                    'rb.sale_id',//销售人  技师ID
                    'rb.sale_type',//销售人  技师ID
                    'rb.people_num',//订房人数  +
                    'rb.single_commission',//单人提成  +
                    'rb.total_commission',//总提成  +
                    'rb.content',//备注   +
                    'rb.create_id',//操作人
                    'rb.update_time',//操作时间
                    'rb.create_time',//操作时间
                ])->leftJoin('qt_users u', 'u.id = rb.sale_id')
                ->where('rb.store_id', $this->userInfo['store_id']);
            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('rb.create_time', $startTime, $endTime);
            }
            //房间号
            if (!empty($post['room_id'])) {
                $query->where('rb.room_id', $post['room_id']);
            }
            //销售人
            if (!empty($post['sale_id'])) {
                $query->where('rb.sale_id', $post['sale_id']);
            }
            //销售类型
            if (!empty($post['sale_type'])) {
                $query->where('rb.sale_type', $post['sale_type']);
            }
            //状态
            if (!empty($post['status'])) {
                $query->where('rb.status', $post['status']);
            }

            $list = $query->order('id desc')->paginate($limit)->each(function ($item) {

                $item['sale_type_name'] = !empty($item['sale_type']) ? CommonConstant::$room_appointment_sale_type[$item['sale_type']] : '';
                $item['sale_name'] = Db::name('qt_users')->where('id', $item['sale_id'])->value('real_name') ?? '';
                $item['updater'] = Db::name('qt_users')->where('id', $item['create_id'])->value('real_name') ?? '';
                $item['room_name'] = Db::name('qt_room')->where('id', $item['room_id'])->value('name') ?? '';
                return $item;
            })->toArray();
            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);

    }

    /**
     * 编辑订房明细列表
     */
    public function update_room_appointment()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();

            if (empty($post['id'])) {
                $this->result('订房ID不能为空', '', 204);
            }
            if (empty($post['room_id'])
                || empty($post['time_out'])
                || empty($post['mobile'])
                || empty($post['sale_type'])
                || empty($post['sale_id'])
            ) {
                $this->result('参数不能为空', '', 204);
            }

            $appointment = Db::name('qt_room_book')
                ->where('store_id', $this->userInfo['store_id'])
                ->where('id', $post['id'])->find();
            if (empty($appointment)) {
                $this->result('没有找到对应ID的数据', '', 204);
            }

            Db::name('qt_room_book')->where('id', $post['id'])->update($post);

            $this->result('修改成功', '', 200);
        }

        $this->result('无效的请求', '', 204);

    }

    public function get_appointment()
    {

        if ($this->request->isPost()) {

            $post = $this->request->post();
            $appointment = Db::name('qt_room_book')
                ->where('store_id', $this->userInfo['store_id'])
                ->where('id', $post['id'])->find();
            if (empty($appointment)) {
                $this->result('没有找到对应ID的数据', '', 204);
            }
            $this->result('查询成功', $appointment, 200);
        }

        $this->result('无效的请求', '', 204);
    }

    /**
     * 充值明细列表
     */
    public function recharge_info_list()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            $query = Db::name('qt_viper_card_recharge_log')->alias('log')
                ->field([
                    'log.id',//id
                    'cid.mobile',//手机号1
                    'cid.viper_type_id',//卡类型1
                    'log.order_no',//充值单号
                    'cid.name',//会员名1
                    'log.store_id',//所在门店1
                    'log.amount',//充值金额1
                    'log.give_amount',//赠送金额
                    'log.recharge_tempid',//充值模板1
                    'log.card_type',//充值模板1
                    'log.card_amount',//入卡金额+
                    //'log.send_voucher_id',//赠送电子券+
                    'log.integral',//赠送积分1
                    'log.pay_content',//付款方式1
                    'log.sale_id',//销售人+
                    'log.commission',//提成+
                    'cid.card_no',//卡号1
                    'log.payee',//收款人+
                    'log.collection_time',//收款时间+
                    'user.group',//销售类型
                    'log.pay_status',//状态
                    'log.coupon_name',//赠送电子券
                    'log.recharge_content',//充值备注
                ])
                ->leftJoin('qt_viper_card cid', 'cid.id=log.viper_card_id')
                ->leftJoin('qt_users user', 'user.id = log.sale_id');
            //->leftJoin('qt_pay_type p', 'p.id = log.pay_type');

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('log.create_time', $startTime, $endTime);
            }

            //单号
            if (!empty($post['order_no'])) {
                $query->where('log.order_no', $post['order_no']);
            }

            //充值模板查询
            if (!empty($post['recharge_tempid'])) {
                $query->where('log.recharge_tempid', $post['recharge_tempid']);
            }

            //手机
            if (!empty($post['mobile'])) {
                $query->where('cid.mobile', $post['mobile']);
            }

            //卡号
            if (!empty($post['card_no'])) {
                $query->where('cid.card_no', $post['card_no']);
            }

            //卡片类型ID
            if (!empty($post['viper_type_id'])) {
                $query->where('cid.viper_type_id', $post['viper_type_id']);
            }
            //付款方式
            if (!empty($post['pay_type'])) {
                $query->whereLike('log.pay_content', '%' . $post['pay_type'] . '%');
            }
            //销售人
            if (!empty($post['is_have_sale'])) {
                if ($post['is_have_sale'] == 1) {
                    $query->where('log.sale_id', '<>', 0);
                } else {
                    $query->where('log.sale_id', '=', 0);
                }
            }

            //销售人
            if (!empty($post['sale_id'])) {
                $query->where('log.sale_id', '=', $post['sale_id']);
            }

            //销售类型
            if (!empty($post['sale_type'])) {
                $query->where('user.group', $post['sale_type']);
            }

            //是否撤销1是2否
            if (!empty($post['is_revoke'])) {
                $query->where('log.is_revoke', $post['is_revoke']);
            }
            //是否首充 1 首充 2 续充
            if (!empty($post['is_first'])) {
                $query->where('log.is_first', $post['is_first']);
            }
            $query->where('log.store_id', $this->userInfo['store_id']);
            $list = $query->order('log.id desc')->paginate($limit)->each(function ($item) {
                $item['payee_name'] = Db::name('qt_users')->where('id', $item['payee'])->value('real_name');
                $item['sale_name'] = Db::name('qt_users')->where('id', $item['sale_id'])->value('real_name');
                $item['viper_type_name'] = Db::name('qt_viper_type')->where('id', $item['viper_type_id'])->value('name');
                $item['store_name'] = Db::name('qt_store')->where('id', $item['store_id'])->value('name');

                if ($item['card_type'] == 1) {
                    $item['recharge_temp_name'] = Db::name('qt_viper_recharge_template')->where('id', $item['recharge_tempid'])->value('name');
                }
                if ($item['card_type'] == 2) {
                    $item['recharge_temp_name'] = Db::name('qt_viper_number_card_template')->where('id', $item['recharge_tempid'])->value('name');
                }
                if ($item['card_type'] == 3) {
                    $item['recharge_temp_name'] = Db::name('qt_viper_month_card_template')->where('id', $item['recharge_tempid'])->value('name');
                }
                return $item;
            });

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);


    }
    /**
     * 修改充值明细列表
     */
    public function update_recharge_info()
    {
        $post = $this->request->post();
        if ($this->request->isPost() && !empty($post['id']) && !empty($post['type'])) {

            $is_have = Db::name('qt_viper_card_recharge_log')
                ->where('store_id', $this->userInfo['store_id'])
                ->where('id', $post['id'])->find();
            if (empty($is_have)) {
                $this->result('没有找到对应ID的数据', '', 204);
            }

            $recharge_log = Db::name('qt_viper_card_recharge_log')->where('id', $post['id'])->find();

            $data = [];
            if ($post['type'] == 1) {//修改支付方式
                if (!empty($post['pay_type'])) {
                    $payCount = Db::name('qt_pay_type')->where(['id' => $post['pay_type']])->find();
                    $json[0]['pay_id'] = $payCount['id'];
                    $json[0]['pay_type'] = $payCount['name'];
                    $json[0]['pay_money'] = $recharge_log['card_amount'];
                    $json = json_encode($json);
                    $data['pay_content'] = $json;
                }

                if (empty($data)) {
                    $this->result('参数错误', '', 204);
                }
                Db::name('qt_viper_card_recharge_log')->where('id', $post['id'])->update($data);
            }

            if ($post['type'] == 2) {//修改提成销售人

                if (!empty($post['sales_person_data'])) {
                    $post['sales_person_data'] = json_decode($post['sales_person_data'],true);
                    $sales_person_data = $post['sales_person_data'];
                    $many_people_commission =  array_sum(array_column($sales_person_data['list'],'commission'));
                    $sale_id =  implode(',',array_column($sales_person_data['list'],'commission_user_id'));
                    $group_id =  implode(',',array_column($sales_person_data['list'],'group_id'));

                    $recharge_log = Db::name('qt_viper_card_recharge_log')->where('id', $post['id'])->find();
                    $viper_card_data = Db::name('qt_viper_card')->where('id',$recharge_log['viper_card_id'])->find();
                    $post['template_id'] = $recharge_log['recharge_tempid'];
                    $post['store_id'] = $recharge_log['store_id'];
                    $post['viper_type_id'] = $viper_card_data['viper_type_id'];
                    $post['template_type'] = $recharge_log['card_type'];
                    $viper_card_id = $recharge_log['recharge_tempid'];

                    $data['sale_id'] = $sale_id;
                    $data['group_id'] = $group_id;
                    $data['commission'] = $many_people_commission;
                    $data['many_people_commission']= json_encode($post['sales_person_data'],JSON_UNESCAPED_UNICODE);

                    Db::name('qt_commission_log')->where('order_project_id', $post['id'])->whereIn('type',[3,4,5,7])->delete();

                    Db::name('qt_viper_card_recharge_log')->where('id', $post['id'])->update($data);
                    //充值提成
                    $op_type = [1=>4, 2=>5, 3=>7];//操作类型  1 项目 2 商品 3 售卡 4 充值 5 次卡 6 副项 7 月卡
                    \app\common\model\ViperCard::rechargeCommissionMany($post, $recharge_log['id'], $viper_card_id,$op_type[$recharge_log['type']],$recharge_log['is_first']);

                }else {

                    $sale_id = !empty($post['sale_id']) ? $post['sale_id'] : 0;
                    $group_id = !empty($sale_id) ? Db::name('qt_users')->where('id',$sale_id)->value('group') : 0;

                    $data['sale_id'] = $sale_id;
                    $data['commission'] = $post['commission'];

                    $log_data = [];
                    if ($group_id == 1) {//1技师 2楼面
                        $log_data['user_id'] = $post['sale_id'];
                        $log_data['commission'] = $post['commission'];
                    }else {
                        $log_data['cashier_id'] = $post['sale_id'];
                        $log_data['receiver'] = $post['commission'];
                    }
                    if (empty($data)) {
                        $this->result('参数错误', '', 204);
                    }
                    Db::name('qt_commission_log')->where('order_project_id', $post['id'])->update($log_data);
                    Db::name('qt_viper_card_recharge_log')->where('id', $post['id'])->update($data);
                }

            }

            if ($post['type'] == 3) {//撤销
                if (!empty($post['is_revoke']) && $post['is_revoke'] == 1) {
                    $data['is_revoke'] = 1;
                    $data['revoke_remarks'] = $post['revoke_remarks'] ?? '';
                }

                //次卡月卡使用记录
                if ($recharge_log['card_type'] == 2 || $recharge_log['card_type'] == 3) {
                    //$card_type = $recharge_log['card_type'] == 2 ? 1 : 2;// 1次卡 2月卡
                    $card_use_log = Db::name('qt_viper_card_item')
                        ->where('recharge_log_id',$recharge_log['id'])
                        ->where('status',0)->find();
                    if (empty($card_use_log)) {
                        $this->result('会员卡充值之后，已经有消费记录了，不能撤销', '', 204);
                    }
                } else {
                    //如果会员卡使用不择不能撤销
                    $check_out = Db::name('qt_check_out')
                        ->where('viper_card_id', $recharge_log['viper_card_id'])
                        ->where('cashier_time', '>', $recharge_log['create_time'])
                        ->where('is_revoke', 2)->find();
                    if (!empty($check_out)) {
                        $this->result('会员卡充值之后，已经有消费记录了，不能撤销', '', 204);
                    }
                }

                Db::name('qt_commission_log')->where('order_project_id', $post['id'])->whereIn('type',[3,4,5,7])->delete();

                //普通卡更新会员卡ID的金额
                if ($recharge_log['card_type'] == 1) {
                    $card_model = Db::name('qt_viper_card')->where('id', $recharge_log['viper_card_id'])->find();
                    Db::name('qt_viper_card')->where('id', $recharge_log['viper_card_id'])
                        ->update([
                            'amount' => $card_model['amount'] - $recharge_log['amount'],
                            'total_amount' => $card_model['total_amount'] - $recharge_log['amount'],
                            'give_amount' => $card_model['give_amount'] - $recharge_log['give_amount'],
                            'give_total_amount' => $card_model['give_total_amount'] - $recharge_log['give_amount'],
                            'score' => $card_model['score'] - $recharge_log['integral']
                        ]);
                }

                //冻结次卡
                if ($recharge_log['card_type'] == 2) {
                    Db::name('qt_viper_card_item')
                        ->where([
                            'viper_card_id' => $recharge_log['viper_card_id'],
                            'number_template_id' => $recharge_log['recharge_tempid']])
                        ->update(['status' => 3]);
                }

                //冻结月卡
                if ($recharge_log['card_type'] == 3) {
                    Db::name('qt_viper_card_item')
                        ->where([
                            'viper_card_id' => $recharge_log['viper_card_id'],
                            'month_template_id' => $recharge_log['recharge_tempid']
                        ])
                        ->update(['status' => 3]);
                }


                if (empty($data)) {
                    $this->result('参数错误', '', 204);
                }
                Db::name('qt_viper_card_recharge_log')->where('id', $post['id'])->update($data);

                Db::name('qt_check_out_pay')->where('check_out_id', $post['id'])->delete();

            }

            $this->result('修改成功', '', 200);
        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * 会员卡次卡/月卡充值明细列表
     */
    public function viper_card_recharge_list()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;

            $query = Db::name('qt_viper_card_recharge_log')
                ->alias('log')
                ->field([
                    'log.id',//单号1
                    'cid.mobile',//手机号1
                    'cid.viper_type_id',//卡类型1
                    'cid.name',//会员名1
                    'log.store_id',//所在门店1
                    'log.amount',//充值金额1
                    'log.recharge_tempid',//充值模板1
                    'log.give_amount',//手动赠送+
                    'log.card_amount',//入卡金额+
                    'log.coupon_name',//赠送电子券+
                    'log.integral',//赠送积分1
                    'log.pay_content',//付款方式1
                    'log.sale_id',//销售人+
                    'user.group',//销售人+
                    'log.card_type',//卡类型+
                    'log.order_no',//单号+
                    'cid.card_no',//卡号1
                    'log.payee',//收款人+
                    'log.collection_time',//收款时间+
                    'log.is_revoke',//收款时间+
                    'log.recharge_temp_name',//模版名称+
                    'log.coupon_name',//优惠券+
                    'item.id as card_item_id',//收款时间+
                ])
                ->leftJoin('qt_viper_card cid', 'cid.id=log.viper_card_id')
                ->leftJoin('qt_users user', 'user.id = log.sale_id')
                ->leftJoin('qt_viper_card_item item', 'item.recharge_log_id = log.id')
                ->where('log.store_id', $this->userInfo['store_id'])
                ->whereIn('log.card_type', [2, 3]);//卡片类型 1普通卡 2次卡 3月卡

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('log.create_time', $startTime, $endTime);
            }

            //销售人
            if (!empty($post['sale_id'])) {
                $query->where('log.sale_id', $post['sale_id']);
            }

            //单号
            if (!empty($post['id'])) {
                $query->where('log.id', $post['id']);
            }
            //手机
            if (!empty($post['mobile'])) {
                $query->where('cid.mobile', $post['mobile']);
            }

            //卡号
            if (!empty($post['card_no'])) {
                $query->where('cid.card_no', $post['card_no']);
            }

            //卡片类型ID
            if (!empty($post['viper_type_id'])) {
                $query->where('cid.viper_type_id', $post['viper_type_id']);
            }

            //销售类型
            if (!empty($post['sale_type'])) {
                $query->where('user.group', $post['sale_type']);
            }

            //销售类型
            if (!empty($post['is_revoke'])) {
                $query->where('log.is_revoke', $post['is_revoke']);
            }

            //充值类型不能为空
            if (!empty($post['pay_status']) && $post['pay_status'] == 3) {
                $query->where('log.pay_status', 3);
            } else {
                $query->where('log.pay_status', '<>', '3');
            }

            $list = $query->order('log.id desc')->paginate($limit)->each(function ($item) {


                if ($item['card_type'] == 2) {
                    $card_data = Db::name('qt_viper_month_card_template')->field('name,amount,rate')->where('id', $item['recharge_tempid'])->find();
                }

                if ($item['card_type'] == 3) {
                    $card_data = Db::name('qt_viper_number_card_template')->field('name,amount,rate')->where('id', $item['recharge_tempid'])->find();
                }
                $item['template_amount'] = !empty($item['amount']) ? $item['amount'] : '';
                $item['template_rate'] = !empty($item['commission']) ? $item['commission'] : '';
                $item['template_name'] = !empty($item['recharge_temp_name']) ? $item['recharge_temp_name'] : '';

                $item['payee_name'] = Db::name('qt_users')->where('id', $item['payee'])->value('real_name') ?? '';
                $item['sale_name'] = Db::name('qt_users')->where('id', $item['sale_id'])->value('real_name') ?? '';
                $item['viper_type_name'] = Db::name('qt_viper_type')->where('id', $item['viper_type_id'])->value('name') ?? '';
                $item['store_name'] = Db::name('qt_store')->where('id', $item['store_id'])->value('name') ?? '';
                return $item;
            })->toArray();

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * 会员卡补录次卡/月卡列表
     */
    public function viper_card_input_list()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;

            $query = Db::name('qt_viper_card_number_supplement')
                ->alias('supplement')
                ->field([
                    'supplement.id',
                    'supplement.viper_card_id',
                    'supplement.viper_type_id',
                    'supplement.template_id',
                    'supplement.create_time',
                    'supplement.business_type',
                    'supplement.creater',
                    'card.card_no',
                    'card.mobile',
                ])
                ->leftJoin('qt_viper_card card', 'supplement.viper_card_id=card.id')
                ->where('supplement.is_revoke=2');

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('supplement.create_time', $startTime, $endTime);
            }

            //销售人
            if (!empty($post['mobile'])) {
                $query->where('card.mobile', $post['mobile']);
            }

            //卡号
            if (!empty($post['card_no'])) {
                $query->where('card.card_no', $post['card_no']);
            }

            $query->where('card.store_id', $this->userInfo['store_id']);
            $list = $query->order('supplement.id desc')->paginate($limit)->each(function ($val) {

                if ($val['business_type'] == 1) {
                    $item_data = Db::name('qt_viper_number_card_template_item')->where('number_card_template_id', $val['template_id'])->select();
                }
                if ($val['business_type'] == 2) {
                    $item_data = Db::name('qt_viper_month_card_template_item')->where('month_template_id', $val['template_id'])->select();
                }

                $project_item = '';
                foreach ($item_data as $k => $v) {
                    if (!empty($v['item_type']) && $v['item_type'] == 'goods') {
                        $name = Db::name('qt_goods')->where('id', $v['item_id'])->value('name');
                        $project_item .= $name . ',';
                    }

                    if (!empty($v['item_type']) && $v['item_type'] == 'project') {
                        $name = Db::name('qt_project')->where('id', $v['item_id'])->value('name');
                        $project_item .= $name . ',';
                    }
                }

                $val['project_item'] = rtrim($project_item, ',');
                $val['user_name'] = Db::name('qt_users')->where('id', $val['creater'])->value('real_name');
                $val['viper_type_name'] = Db::name('qt_viper_type')->where('id', $val['viper_type_id'])->value('name');
                return $val;
            });

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);
    }

    /**
     * 撤销 会员卡补录次卡/月卡列表
     */
    public function revoke_viper_card_input()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (  empty($post['id'])) {
                $this->result('参数不能为空', '', 204);
            }
            $supplement = Db::name('qt_viper_card_number_supplement')->where('id', $post['id'])->find();
            if (empty($supplement)) {
                $this->result('没有找到数据', '', 204);
            }
            Db::name('qt_viper_card_number_supplement')->where('id', $post['id'])->update(['is_revoke' => 1]);

            if ($supplement['business_type'] == 1) {
                Db::name('qt_viper_card_item')
                    ->where(['viper_card_id' => $supplement['viper_card_id'], 'number_template_id' => $supplement['template_id']])
                    ->update(['status' => 3]);
            } else {
                Db::name('qt_viper_card_item')
                    ->where(['viper_card_id' => $supplement['viper_card_id'], 'month_template_id' => $supplement['template_id']])
                    ->update(['status' => 3]);
            }

            $this->result('操作成功', '', 200);
        }
        $this->result('无效的请求', '', 204);

    }

    /**
     * 获取会员信息接口
     */
    public function get_viper_card_info()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();

            $where = [];
            if (empty($post['params']) && empty($post['type'])) {
                $this->result('参数不能为空', '', 204);
            }
            //参数类型 1手机号 2卡号
            if (!empty($post['type'])) {
                if ($post['type'] == 1) {
                    $where[] = ['mobile', '=', $post['params']];
                }
                if ($post['type'] == 2) {
                    $where[] = ['card_no', '=', $post['params']];
                }
            }

            //if ( !empty($post['mobile'])) {
            //    $where[] = ['mobile','=',$post['mobile']];
            //}

            $data = Db::name('qt_viper_card')
                ->where($where)
                ->where('store_id', $this->userInfo['store_id'])
                ->find();

            $this->result('查询成功', $data, 200);
        }

        $this->result('无效的请求', '', 204);
    }


    /**
     * 录入会员补录记录次卡月卡
     */
    public function entry_viper_card()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (empty($post['viper_type_id']) || empty($post['viper_card_id'])) {
                $this->result('参数错误', '', 204);
            }
            if (!empty($post['number'])) {
                //[{"name":"2","expire":"365","number":"10","item":"[{\"data\":[\"project-17\",\"project-19\"],\"value\":[\"脚部按摩\",\"生姜足浴\"]}]"}]
                NumberCardTemplate::supplementCreateByTemplate($post['number'],$this->userInfo,$post['viper_type_id'],$post['viper_card_id']);
            }
            if (!empty($post['month'])) {
                MonthCardTemplate::supplementCreateByTemplate($post['month'],$this->userInfo,$post['viper_type_id'],$post['viper_card_id']);
            }

            $this->result('补录成功', '', 200);
        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * 会员补录信息列表
     */
    public function viper_supplement_list()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;

            $where = [];
            //手机
            if (!empty($post['mobile'])) {
                $where[] = ['cid.mobile', '=', $post['mobile']];
            }

            //卡号
            if (!empty($post['card_no'])) {
                $where[] = ['cid.card_no', '=', $post['card_no']];
            }

            //销售人
            if (!empty($post['user_id'])) {
                $where[] = ['sup.user_id', '=', $post['user_id']];
            }

            //旧卡号
            if (!empty($post['old_card_no'])) {
                $where[] = ['sup.old_card_no', '=', $post['old_card_no']];
            }


            $query = Db::name('qt_viper_card_supplement')
                ->alias('sup')
                ->field([
                    'sup.id',//id
                    'cid.mobile',//手机号1
                    'cid.card_no',//卡号1
                    'cid.viper_type_id',//卡类型
                    'sup.old_card_no',//原卡号
                    'sup.give_money',//赠送金额
                    'sup.money',//补录金额
                    'sup.user_id',//补录人id
                    'sup.create_time',//补录时间
                    'user.real_name',//补录人
                    'sup.is_revoke',//是否撤销
                ])
                ->leftJoin('qt_viper_card cid', 'cid.id = sup.card_id')
                ->leftJoin('qt_users user', 'user.id = sup.user_id')
                ->where('sup.is_revoke', 2)
                ->where('user.store_id', $this->userInfo['store_id'])
                ->where($where);

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereTime('sup.create_time', 'between', [$startTime, $endTime]);
            }

            $list = $query->order('sup.id desc')->paginate($limit)->each(function ($item) {
                $item['viper_type_name'] = Db::name('qt_viper_type')->where('id', $item['viper_type_id'])->value('name') ?? '';
                return $item;
            })->toArray();

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);
    }

    /**
     * 会员补录信息撤销操作
     */
    public function revoke_supplement()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (empty($post['id'])) {
                $this->result('ID不能为空', '', 204);
            }
            $supplement = Db::name('qt_viper_card_supplement')
                ->where('id', $post['id'])->find();
            if (empty($supplement)) {
                $this->result('没有查询到该ID的数据', '', 204);
            }
            Db::name('qt_viper_card_supplement')->where('id', $post['id'])->update(['is_revoke' => 1]);

            $this->result('撤销成功', '', 200);
        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * 会员转账信息
     *
     * @throws \think\exception\DbException
     */
    public function viper_card_giro()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;

            $query = Db::name('qt_viper_card_giro')->where('store_id', $this->userInfo['store_id']);

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('create_time', $startTime, $endTime);
            }

            $list = $query->paginate($limit)->each(function ($item) {
                $item['user_name'] = Db::name('qt_users')->where('id', $item['user_id'])->value('real_name') ?? '';
                return $item;
            });

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * PA净房数据列表
     */
    public function room_clear_list()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            $query = Db::name('qt_room_clear')
                ->alias('clear')
                ->field([
                    'clear.id',
                    'clear.room_id',
                    'clear.user_id',
                    'clear.clear_bed',
                    'clear.clear_user_id',
                    'clear.open_time',
                    'clear.clear_time',
                    'clear.single_commission',
                    'clear.total_commission',
                    'room.name room_name',
                    'room.type_id',
                    'room.bed_num',
                    'room.clean_rule',
                    'room.clean_price',
                    'type.name room_type_name',
                ])
                ->leftJoin("qt_room room", 'clear.room_id=room.id')
                ->leftJoin('qt_room_type type', 'room.type_id=type.id')
                ->where('clear.store_id', $this->userInfo['store_id']);

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('clear.create_time', $startTime, $endTime);
            }

            if (!empty($post['clear_user_id'])) {
                $query->where('clear.clear_user_id', $post['clear_user_id']);
            }

            if (!empty($post['room_name'])) {
                $query->whereLike('room.name', "%" . $post['room_name'] . "%");
            }

            if (!empty($post['room_type'])) {
                $query->where('room.type_id', $post['room_type']);
            }

            $list = $query->order('clear.id desc')->paginate($limit)->each(function ($item) {
                $item['user_name'] = Db::name('qt_users')->where('id', $item['user_id'])->value('real_name') ?? '';
                $item['clear_user_name'] = Db::name('qt_users')->where('id', $item['clear_user_id'])->value('real_name') ?? '';
                return $item;
            });

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);

    }

    /**
     * 修改净房数据
     */
    public function edit_room_clear_commission()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();

            if (!empty($post['id'])) {

                $is_have = Db::name('qt_room_clear')->where('id', $post['id'])->find();
                if (empty($is_have)) {
                    $this->result('没有找到对应ID的数据', '', 204);
                }

                Db::name('qt_room_clear')->where('id', $post['id'])->update($post);

                $this->result('操作成功', '', 200);
            } else {

                $is_have = Db::name('qt_room_clear')->where('id', $post['id'])->find();
                if (empty($is_have)) {
                    $this->result('没有找到对应ID的数据', '', 204);
                }
            }
        }

        $this->result('无效的请求', '', 204);

    }

    /**
     * 获取净房编辑数据接口
     */
    public function get_room_commission()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();

            if (empty($post['id'])) {

                $this->result('id不能为空', '', 204);
            }

            $room = Db::name('qt_room_clear')->where('id', $post['id'])->find();
            if (empty($room)) {
                $this->result('没有找到对应ID的数据', '', 204);
            }

            $this->result('操作成功', $room, 200);
        }

        $this->result('无效的请求', '', 204);
    }


    /**
     * 会员消费列表
     */
    public function viper_card_use_list()
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;

            $query = Db::name('qt_check_out')
                ->alias('out')
                ->field([
                    'card.mobile',//手机号
                    'out.viper_card_id',//卡号
                    'card.viper_type_id',//卡类型
                    'type.name viper_type_name',//卡类型名称
                    'card.name viper_name',//会员名称
                    'out.store_id',//会员所在门店
                    'out.order_no',//结账单号
                    'out.pay_content',//付款方式
                    'out.uv_id',//会员
                    'out.total_money',//划卡
                    'out.cashier_time',//结账时间
                    'out.cashier_id',//结账人
                    'card.status',//状态
                ])
                ->leftJoin("qt_viper_card card", 'card.id=out.viper_card_id')
                ->leftJoin('qt_viper_type type', 'card.viper_type_id=type.id')
                ->where('out.store_id', $this->userInfo['store_id'])->whereNotNull('card.id');

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('out.cashier_time', $startTime, $endTime);
            }

            if (!empty($post['mobile'])) {
                $query->where('card.mobile', $post['mobile']);
            }

            if (!empty($post['viper_card_id'])) {
                $query->where('out.viper_card_id', $post['viper_card_id']);
            }

            if (!empty($post['viper_type_id'])) {
                $query->where('card.viper_type_id', $post['viper_type_id']);
            }

            $list = $query->order('out.id desc')->paginate($limit)->each(function ($item) {
                //$item['pay_content'] = '[{"pay_type":"微信","pay_money":100},{"pay_type":"支付宝","pay_money":111}]';

                $item['cashier_name'] = Db::name('qt_users')->where('id', $item['cashier_id'])->value('real_name') ?? '';
                $pay_content_json = json_decode($item['pay_content'], true);
                if (!empty($pay_content_json)) {

                    $item['pay_content'] = implode(',', array_column($pay_content_json, 'pay_type'));
                }
                $status = [1 => '正常', 2 => '禁用', 3 => '挂失', 4 => '删除'];//1=正常，2=禁用，3=挂失  4= 删除

                $item['status'] = $status[$item['status']] ?? '';
                $item['store_name'] = Db::name('qt_store')->where('id', $item['store_id'])->value('name') ?? '';
                $item['card_no'] = Db::name('qt_viper_card')->where('id', $item['viper_card_id'])->value('card_no') ?? '';
                $item['money_log'] = Db::name('qt_viper_money_log')
                    ->where('id', $item['order_no'])
                    ->where(['type' => 1, 'check' => 1, 'flag' => 3,])
                    ->value('money');
                return $item;
            });

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);

    }

    /**
     * 获取活动价格列表
     *
     * @throws \think\exception\DbException
     */
    public function get_check_out_activity_list()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            $query = Db::name('qt_check_out_item')
                ->alias('oa')
                ->field([
                    'out.order_no',//结账单号
                    'da.name as activity_name',//活动名称
                    'oa.activity_id',//活动id
                    'oa.link_id',//活动项目ID
                    'oa.link_type',//活动项目类型，1项目 2商品
                    'oa.receivable',//应收金额
                    'oa.actual_receipt',//实际收款金额
                    'oa.deduction_price',//抵扣金额
                    'out.cashier_id',//结账人
                    'out.cashier_time',//结账时间
                    'out.cashier_remarks',//结账备注
                    'u.group',//人员类型 1技师 2楼面
                    'u.real_name',//结账人姓名
                    'u.group',//人员类型
                ])
                ->leftJoin('qt_check_out out', 'out.id=oa.check_out_id')
                ->leftJoin('qt_data_activity da', 'da.id=oa.activity_id')
                ->leftJoin('qt_users u', 'u.id=out.cashier_id')
                ->where('out.store_id', $this->userInfo['store_id']);

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $startTime = strtotime($post['start_time']);
                $endTime = strtotime($post['end_time']);
                $query->whereBetweenTime('cashier_time', $startTime, $endTime);
            }

            //结账人
            if (!empty($post['cashier_id'])) {
                $query->where('out.cashier_id', $post['cashier_id']);
            }

            //项目类型,商品项目ID
            if (!empty($post['link_type']) && !empty($post['link_id'])) {
                $query->where('oa.link_type', $post['link_type']);
                $query->where('oa.link_id', $post['link_id']);

            }

            //活动项目ID
            if (!empty($post['activity_id'])) {
                $query->where('oa.activity_id', $post['activity_id']);
            }

            $list = $query->order('oa.id desc')->paginate($limit)->each(function ($item) {

                $item['project_name'] = '';
                if ($item['link_type'] == 1) {
                    $item['project_name'] = Db::name('qt_project')->where('id', $item['link_id'])->value('name') ?? '';
                }
                if ($item['link_type'] == 2) {
                    $item['project_name'] = Db::name('qt_goods')->where('id', $item['link_id'])->value('name') ?? '';
                }

                return $item;
            });

            $this->result('查询成功', $list, 200);
        }

        $this->result('无效的请求', '', 204);
    }

    /**
     * 会员补录上传文件
     */
    public function upload_viper_card(){
        $file = request()->file("file");
        if (empty($file)) {
            $this->result('请上传文件','',204);
        }
        if ($file) {
            //最大3m
            $info = $file->validate(['size' => 1048*1024])->move('../public/uploads/excel');
            if ($info) {
                $imgurl = 'uploads/' . $info->getSaveName();
                $data1 = array(
                    'path' => $imgurl,
                    'name' => $info->getSaveName(),
                    'module' => 'index',
                    'md5' => md5($info->getSaveName()),
                    'thumb' => '',
                    'url' => '1',
                    'siza' => '0',
                    'mime' => $info->getExtension(),
                    'ext' => '0',
                    'sha1' => sha1($info->getSaveName()),
                    'create_time' => time(),
                    'update_time' => time(),
                );
                $re = Db::name('admin_attachment')->insertGetId($data1);
                if ($re) {
                    $viperCardSupplementModel = new ViperCardSupplement();
                    $result = $viperCardSupplementModel::getExcelData($info->getSaveName(),$this->userInfo);
                    $this->result('上传成功',$result,200);
                } else {
                    $this->result('上传失败，请稍后重试','',204);
                }
            } else {
                //上传失败获取错误信息
                $this->result($file->getError(),'',204);
            }
        } else {
            $this->result('请上传文件','',204);
        }


    }


}
